<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<body>
		<table>
			<tr>
				<td>
					<div id="addOrderButton"></div>
				</td>
				<td>
					<div id="deleteOrderButton"></div>
				</td>
			</tr>
		</table>
		<div class="gridContainer" style="width:100%;height:300px">
			<h2>Your recent orders of instrument:</h2>
			<div id="instrumentOrdersGrid" style="width:100%;height:250px">
			</div>
		</div>
	</body>
	<script type="text/javascript">
		dojo.addOnLoad( function( ) {
			var instrumentOrdersLayout = [
				{ field: "orderId", 	name: "Id",				width: "auto"},
				{ field: "projectName", name: "Project name", 	width: "auto"},
			    { field: "userId", 		name: "Order owner", 	width: "auto"},
			    { field: "date", 		name: "Order date", 	width: "auto"},
			    { field: "startSlot", 	name: "Start slot", 	width: "auto"},
			    { field: "slotsCount", 	name: "Slots count", 	width: "auto"},
			    { field: "total", 		name: "Total price", 	width: "auto"}
			];
			var ordersGrid = new dojox.grid.DataGrid( {
                query: { orderId: '*' },
                structure: instrumentOrdersLayout
            }, 'instrumentOrdersGrid');
            ordersGrid.startup( );
			dojo.connect( dijit.byId( "instrumentsGrid"), "onSelected", function( ) {
				var initialized = false;
				return function( event) {
					var ordersStore = new dojo.data.ItemFileWriteStore( { 
						url : "/FinalProject/app/ordersManagement?ordersOp=order_instrument_orders_list&instrumentId=" + this.selection.getSelected()[0].id, 
						clearOnClose: true, 
						jsId:"ordersStore"});
					ordersGrid.setStore( ordersStore);
					if ( initialized == false) {
						initialized = true;
						var addOrderButton = new dijit.form.Button( {
							label 	: "Add Order",
							onClick : function( event) {
								var orderDialog = new dijit.Dialog( {
									id : "orderDialog",
									title : "Make new order",
									href : "/FinalProject/templates/orders/newOrder.jsp",
									onCancel : function( event) {
										this.destroyRecursive( );
									} 
								});
								orderDialog.onLoad = function( event) {
									dojo.connect( dijit.byId( "selectDate"), "onChange", function( event) {
										var url = "/FinalProject/app/ordersManagement?ordersOp=order_get_slots&instrumentId="+dijit.byId( "instrumentsGrid").selection.getSelected()[0].id[0];
										availableStore.url = url + "&date=" + this.toString( );
										dijit.byId( "selectStartSlot").store.close( );
									});
								};
								orderDialog.show( );
							}
						}, "addOrderButton");
						var upateOrderButton = new dijit.form.Button( {
							label 	: "Delete order",
							onClick : function( event) {
								if ( dijit.byId( "instrumentOrdersGrid").selection.getSelected( ).length == 0) {
									dijit.byId( "toaster").setContent( "Select order from the grid.", "fatal");
									dijit.byId( "toaster").show( );
									return;
								}
								var orderId = dijit.byId( "instrumentOrdersGrid").selection.getSelected( )[0].orderId[0];
								dojo.xhrPost( {
									url : "/FinalProject/app/ordersManagement?ordersOp=order_delete&orderId=" + orderId,
									load : function( data, ioArgs) {
										console.log( "Load...");
										if ( data == "ERROR") {
											console.log( "Load error.");
											dijit.byId( "toaster")
													.setContent( "Backend error deleting order. Please try later.", "fatal")
													.show( );
											return;
										}
										dijit.byId( "instrumentOrdersGrid").removeSelectedRows( );
									}
								});
							} 
						}, "deleteOrderButton");
					}
				};
			}( ));
		}); 
	</script>
</html>